package org.eiahe.hr.interview.controller;

import lombok.extern.slf4j.Slf4j;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.interview.domain.dto.InterviewQuestionBankDTO;
import org.eiahe.hr.interview.domain.vo.InterviewQuestionBankVo;
import org.eiahe.hr.interview.domain.dto.InterviewQuestionBankQueryDTO;
import org.eiahe.hr.interview.service.InterviewQuestionBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 面试题库管理
 * @author :zhangnn
 * @className :InterviewQuestionBankController
 * @description: 面试题库的增删改查操作
 * @date 2025-08-13 16:45:00
 */
@Slf4j
@RestController
@RequestMapping("/interview/question/bank")
public class InterviewQuestionBankController extends BaseController {
    @Autowired
    private InterviewQuestionBankService questionBankService;

    /**
     * 分页查询题库列表
     */
    @GetMapping("/page")
    public TableDataInfo<InterviewQuestionBankVo> list(InterviewQuestionBankQueryDTO queryDTO, PageQuery pageQuery) {
        TableDataInfo<InterviewQuestionBankVo> page = questionBankService.selectQuestionBankPage(queryDTO, pageQuery);
        return page;
    }

    /**
     * 查询所有可用的题库（未删除的）
     */
    @GetMapping("/all")
    public R<List<InterviewQuestionBankDTO>> getAllEnabled() {
        List<InterviewQuestionBankDTO> list = questionBankService.selectAllEnabled();
        return R.ok(list);
    }

    /**
     * 根据ID查询题库详情
     */
    @GetMapping("/{id}")
    public R<InterviewQuestionBankDTO> getById(@PathVariable Long id) {
        InterviewQuestionBankDTO bank = questionBankService.selectById(id);
        return R.ok(bank);
    }

    /**
     * 新增题库
     */
    @Log(title = "新增题库", businessType = BusinessType.INSERT,linkType = "interview",nameField = "bankName")
    @PostMapping("/create")
    public R<Void> create(@Validated @RequestBody InterviewQuestionBankDTO bankDTO) {
        // 可根据业务需求添加校验（如题库名称唯一性）
        boolean success = questionBankService.insertQuestionBank(bankDTO);
        return success ? R.ok("新增成功") : R.fail("新增失败");
    }

    /**
     * 更新题库
     */
    @Log(title = "更新题库信息", businessType = BusinessType.UPDATE,linkType = "interview",nameField = "bankName")
    @PostMapping("/update")
    public R<Void> update(@Validated @RequestBody InterviewQuestionBankDTO bankDTO) {
        // 可根据业务需求添加校验（如题库名称唯一性，排除当前记录）
        boolean success = questionBankService.updateQuestionBank(bankDTO);
        return success ? R.ok("更新成功") : R.fail("更新失败");
    }

    /**
     * 删除题库（逻辑删除，更新del_flag为1）
     */
    @Log(title = "删除题库信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable Long id) {
        boolean success = questionBankService.deleteQuestionBank(id);
        return success ? R.ok("删除成功") : R.fail("删除失败");
    }

    /**
     * 批量删除题库（逻辑删除）
     */
    @Log(title = "删除题库信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/batch/{ids}")
    public R<Void> batchDelete(@PathVariable String ids) {
        List<Long> idList = Arrays.stream(ids.split(","))
            .map(Long::valueOf)
            .collect(Collectors.toList());
        boolean success = questionBankService.deleteQuestionBankByIds(idList);
        return success ? R.ok("批量删除成功") : R.fail("批量删除失败");
    }
}
